package com.atguigu.bigdata.scala.mytest.chapter07

import scala.io.Source

object Test_WordCount1 {
  def main(args: Array[String]): Unit = {
    val lines: List[String] = Source.fromFile("data/word.txt").getLines().toList
    println(lines)

    val strings: List[String] = lines.flatMap(s => s.split(" "))
    println(strings)

    val map1: Map[String, List[String]] = strings.groupBy(w => w)
    println(map1)

    val wordCount: Map[String, Int] = map1.map(w =>{(w._1,w._2.size)})
    println(wordCount)

    val wordCountG: Map[Int, Map[String, Int]] = wordCount.groupBy(w => w._2)
    println(wordCountG)

    val wordCountM: Map[Int, List[String]] = wordCountG.map(w =>(w._1,w._2.map(s => s._1).toList))
    println(wordCountM)

    val wordList: List[(Int, List[String])] = wordCountM.toList
    println(wordList)

    val tuples: List[(Int, List[String])] = wordList.sortBy(w => w._1)(Ordering.Int.reverse)
    println(tuples)

    val tuples1: List[(Int, List[String])] = tuples.take(3)
    println(tuples1)
  }
}
